import { log } from "../shared/log";
import {
  mkdirpSync,
  pathExistsSync,
  readFileSync,
  stat,
  outputFileSync
} from "fs-extra";
import { BaseConfig } from "./config";
const md5 = require("js-md5");
import { join, resolve } from "path";
import { vue_template } from "../template/vue-tempeleate";
import { react_template } from "../template/react-tempeleate";
import { InterfaceConfigJson, InterfacePageJson } from "../entery/index";
import { typefileType } from "./react-auto";
var process = require("process");

export class Init extends BaseConfig {
  constructor() {
    super();
  }
  ROOT_DIR = join(process.cwd(), "src");
  FILE_DIR = join(this.ROOT_DIR, this.configure_folder);
  FILE_NAME = join(this.FILE_DIR, "model.json");

  init(data: any) {
    let err = "";
    let res = pathExistsSync(this.FILE_DIR);
    if (pathExistsSync(this.FILE_NAME)) {
      log(
        `已经有这个文件了${this.FILE_NAME}文件已经存在，无法重新初始化`,
        "red"
      );
      return;
    }
    if (!res) {
      mkdirpSync(this.FILE_DIR);
    }
    try {
      outputFileSync(this.FILE_NAME, JSON.stringify(data, null, "\t"));
      log("项目初始化成功");
    } catch (error) {
      err = error;
      console.error(error);
    }
    return err;
  }
  getViewsPath() {
    let json: Buffer | any = readFileSync(this.FILE_NAME);
    json = JSON.parse(String(json));
    let filePath = resolve(this.ROOT_DIR, json.pages);
    return filePath;
  }
  getConfigJson(): typefileType {
    let json: Buffer | any = readFileSync(this.FILE_NAME);
    return (json = JSON.parse(String(json)));
  }
  addFile(fileName: string) {
    if (!fileName) {
      log(`请输入路径`, "red");
      return;
    }
    fileName = fileName.replace(/ /g, "");
    let copyFileName = fileName;
    let json: Buffer | any = readFileSync(this.FILE_NAME);
    json = JSON.parse(String(json));
    let filePath = resolve(this.ROOT_DIR, json.pages);
    if (pathExistsSync(filePath)) {
      let dir = resolve(filePath, fileName);
      let fileType = json.mode === "vue" ? "vue" : "jsx";
      if (json.mode === "react" && json.type === "ts") {
        fileType = "tsx";
      }
      fileName = join(dir, `index.${fileType}`);
      if (pathExistsSync(fileName)) {
        log(`文件以及存在请勿重新生成`, "red");
        return;
      } else if (!pathExistsSync(dir)) {
        mkdirpSync(dir);
      }
      this.creatFile(json, copyFileName, fileName, dir, filePath);
    } else {
      log(`没有在src中找到${filePath}路径，请检查model.json中的配置`, "red");
    }
  }
  creatFile(
    json: InterfaceConfigJson,
    copyFileName: string,
    fileName: string,
    dir: string,
    filePath: string
  ) {
    let str = "";
    if (json.mode === "vue") {
      str = vue_template;
    } else if (json.mode === "react") {
      str = react_template;
    }
    let json_data = {
      name: copyFileName,
      path: `/${json.pages}/${copyFileName}`,
      component: `/${json.pages}/${copyFileName}/index`,
      parentPath: null,
      redirect: null,
      id: new Date().getTime(),
      parentId: null,
      sort: 0, 
      children: [],
      query: {},
      "#description": {
        author: "作者名",
        name: "页面描述"
      }
    };
    str = str.replace(/\#name\#/g, copyFileName.replace(/-/g, "_"));
    outputFileSync(fileName, str);
    outputFileSync(
      join(dir, `index.json`),
      JSON.stringify(json_data, null, "\t")
    );
    log(`生成成功${fileName}`, "greyBG");
  }
}
